package com.zhuozhou.jdyc.main.meinfo.activity;

import android.os.Bundle;

import com.zhuozhou.jdyc.R;
import com.zhuozhou.jdyc.api.UserAccountHelper;
import com.zhuozhou.jdyc.base.BaseActivity;
import com.zhuozhou.jdyc.databinding.ActivityAddShipingAddressBinding;
import com.zhuozhou.jdyc.main.meinfo.bean.ShippingAddressBean;
import com.zhuozhou.jdyc.main.meinfo.viewmodel.MeInfoViewModel;
import com.zhuozhou.jdyc.util.apiUtil.StringUtil;
import com.zhuozhou.jdyc.util.log.ToastUtils;

/**
 * Create by Once_ChuXy on 2021/10/20
 * Describe:收货地址
 */
public class AddShippingAddressActivity extends BaseActivity<MeInfoViewModel, ActivityAddShipingAddressBinding> {
    private ShippingAddressBean.ArrayBean data;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_add_shiping_address;
    }

    @Override
    public String setTitleBar() {
        return "收货地址";
    }

    @Override
    public void initView() {
        binding.tvAddAddress.setOnClickListener(v -> {
            if (StringUtil.isEmpty(binding.etName.getText().toString())) {
                ToastUtils.showShort(this, "请输入收货人");
                return;
            }
            if (StringUtil.isEmpty(binding.etPhone.getText().toString())) {
                ToastUtils.showShort(this, "请输入手机号码");
                return;
            }
            if (StringUtil.isEmpty(binding.etLocation.getText().toString())) {
                ToastUtils.showShort(this, "请输入所在地区");
                return;
            }
            if (StringUtil.isEmpty(binding.etDetailedAddress.getText().toString())) {
                ToastUtils.showShort(this, "请输入详细地址");
                return;
            }
            if (data == null)
                mViewModel.postSite(UserAccountHelper.getUserID() + "", binding.etName.getText().toString(), binding.etPhone.getText().toString(),
                        "", binding.etLocation.getText().toString(), binding.etDetailedAddress.getText().toString(), binding.switchChecked.isChecked() ? 1 : 0);
            else
                mViewModel.postSiteUpdate(data.getId(), UserAccountHelper.getUserID() + "", binding.etName.getText().toString(), binding.etPhone.getText().toString(),
                        "", binding.etLocation.getText().toString(), binding.etDetailedAddress.getText().toString(), binding.switchChecked.isChecked() ? 1 : 0);
        });
        mViewModel.SiteMutableLiveData.observe(this, v -> {
            ToastUtils.showShort(v.getMsg());
            if (v.getType() == 1) {
                finish();
            }
        });
        mViewModel.SiteUpdateMutableLiveData.observe(this, v -> {
            ToastUtils.showShort(v.getMsg());
            if (v.getType() == 1) {
                finish();
            }
        });
    }

    @Override
    public void initData(Bundle bundle) {
        if (bundle.getSerializable("data") != null) {
            data = (ShippingAddressBean.ArrayBean) bundle.getSerializable("data");
            binding.etName.setText(data.getName());
            binding.etPhone.setText(data.getPhone());
            binding.etLocation.setText(data.getCity());
            binding.etDetailedAddress.setText(data.getCity_details());
            binding.switchChecked.setChecked(Integer.parseInt(data.getStatus()) == 1);
        }
    }

}
